#!/usr/bin/perl

# This file is part of brsync.

# brsync is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# brsync is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with brsync.  If not, see <https://www.gnu.org/licenses/>.


my $BaseCommand = "rsync";

sub help() {
    print "USAGE:";
    print "$0 URL\n";
    exit;
}

if (not defined $ARGV[0]) {
    &help();
}

system("$BaseCommand $ARGV[0]");
my $Command;
my $key;
my $value;
my $base;

while(1) {
    print "brsync> ";
    chomp($Command = <STDIN>);
    $key = $Command;
    $value = $Command;

    $key =~ s/\s.*$//;
    $value =~ s/[^ ]* //;

    if ($key eq "cd") {

        $base = $base . $value . "/";
        system("rsync $ARGV[0]/$base");




    }
    elsif ($key eq "get") {
        system("rsync --progress -rdt $ARGV[0]/$base/$value .");


    }
    elsif($key eq "ls") {
        system("rsync $ARGV[0]/$base");
    }
    elsif($key eq "exit") {
        exit;
    }
    elsif($key eq "help") {
        print "HELP\n";
        print "cd DIRECTORY - change dir\n";
        print "get FILE - download file\n";
        print "ls - list directory\n";
        print "exit - finish brsync\n";
    }
    else {
        print "Unknown command\n";
    }

}
